﻿package UIComponents
{
	import Classes.GameUtils.MyTipSprite;
	import Classes.GameUtils.ToolTip_Sprite;
	
	import MyComponent.MyButton;
	
	import fl.events.ComponentEvent;
	
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.filters.BitmapFilterQuality;
	import flash.filters.ColorMatrixFilter;
	import flash.filters.GlowFilter;
	import flash.text.TextField;
	
	public class WeaponPanel extends Sprite
	{
		private var _nowPage:int=1;/*当前页数*/
		private var _maxPage:int;/*最大页数*/
		private var _itemsAry:Array;/*物品数组*/
		private var _bulideLv:Number;/*武器建筑等级*/
		private var _isHome:Boolean;/*是否在避难所内*/
		
		public var wqAllBtn:MyButton;
		public var wqDjBtn:MyButton;
		public var wqQsBtn:MyButton;
		public var wqHyBtn:MyButton;
		public var wqWlBtn:MyButton;
		public var wqHmBtn:MyButton;
		
		public var tipSetAry:Vector.<Boolean>;
		
		public function WeaponPanel()
		{
			init();
		}
		
		private function init():void
		{
			this.wqAllBtn=this.getChildByName("allBtn") as MyButton;
			this.wqDjBtn=this.getChildByName("djBtn") as MyButton;
			this.wqQsBtn=this.getChildByName("qsBtn") as MyButton;
			this.wqHyBtn=this.getChildByName("hyBtn") as MyButton;
			this.wqWlBtn=this.getChildByName("wlBtn") as MyButton;
			this.wqHmBtn=this.getChildByName("hmBtn") as MyButton;
			this.tipSetAry	=  new Vector.<Boolean>( [ false, false, false, false, false]);
			inintLister();
		}
		private function inintLister():void
		{
			(this.getChildByName("buttonLeft") as MyButton).addEventListener(ComponentEvent.BUTTON_DOWN,turnLeftHandler);
			(this.getChildByName("buttonRight") as MyButton).addEventListener(ComponentEvent.BUTTON_DOWN,turnRightHandler);
			this.wqAllBtn.addEventListener(ComponentEvent.BUTTON_DOWN,allFun);
			this.wqDjBtn.addEventListener(ComponentEvent.BUTTON_DOWN,djFun);
			this.wqQsBtn.addEventListener(ComponentEvent.BUTTON_DOWN,qsFun);
			this.wqHyBtn.addEventListener(ComponentEvent.BUTTON_DOWN,hyFun);
			this.wqWlBtn.addEventListener(ComponentEvent.BUTTON_DOWN,wlFun);
			this.wqHmBtn.addEventListener(ComponentEvent.BUTTON_DOWN,hmFun);
			this.controlClickFun();
			this.controlXZWqClickFun();
			
			for(var i:uint=1 ;i<5;i++)
			{
				var swp:SellectWeaponsPanel=this.getSelectWeaponListFrame(i);
				ToolTip_Sprite.getInstance().setTip(swp.getChildByName("fm"), new MyTipSprite("武器库"+(i+1)+"等级解锁"), ToolTip_Sprite.DIRECTION_UP);
			}
			
			
		}
		
		/**
		 *加热感 
		 * 
		 */
		public function addTip():void
		{return;
			var i:int;
			for(i=0 ;i<5;i++)
			{
				var swp:SellectWeaponsPanel=this.getSelectWeaponListFrame(i);
				
				if(this.tipSetAry[i]){
					ToolTip_Sprite.getInstance().removeTip(swp);
//					ToolTip_Sprite.getInstance().removeTip(swp.wqNumber);
					this.tipSetAry[i]	=	false;
				}
//				trace(swp.getChildByName("wqNumber"));
//				
//				if( i < this.bulideLv){
//					ToolTip_Sprite.getInstance().setTip(swp.wqNumber, new MyTipSprite("按数字"+i+"键可切换"),ToolTip_Sprite.DIRECTION_UP);
//				}
//				else{
//					ToolTip_Sprite.getInstance().setTip(swp,new MyTipSprite("需要等级"+i+1),ToolTip_Sprite.DIRECTION_UP);
//				}
			}
			
			for(i=this.bulideLv ;i<5;i++)
			{
				var swp:SellectWeaponsPanel=this.getSelectWeaponListFrame(i);
				
				ToolTip_Sprite.getInstance().setTip(swp,new MyTipSprite("需要等级"+i),ToolTip_Sprite.DIRECTION_UP);
				this.tipSetAry[i]	=	true;
			}
			
		}
		
		private function controlBtnState(all:Boolean=false,dj:Boolean=false,qs:Boolean=false,hy:Boolean=false,wl:Boolean=false,hm:Boolean=false):void
		{
			this.wqAllBtn.lockState=all;
			this.wqDjBtn.lockState=dj;
			this.wqQsBtn.lockState=qs;
			this.wqHyBtn.lockState=hy;
			this.wqWlBtn.lockState=wl;
			this.wqHmBtn.lockState=hm;
		}
		
		/**
		 * 所有武器	 
		 * @param e
		 * 
		 */		
		private function allFun(e:ComponentEvent):void
		{
			
			if(FlatInfo.newGuiderQHWq)
			{
				this.dispatchEvent(new PanelEvent(PanelEvent.NOTNEWGUIDESD,true));
				return ;
			}
			this.setItemAry("all");
			controlBtnState(true);
		}
		/**
		 * 电击武器	 
		 * @param e
		 * 
		 */	
		private function djFun(e:ComponentEvent):void
		{
			this.setItemAry("112");
			this.controlBtnState(false,true);
			this.dispatchEvent(new PanelEvent(PanelEvent.ADDARROWTODJ,true));
		}
		/**
		 * 侵蚀武器	 
		 * @param e
		 * 
		 */	
		private function qsFun(e:ComponentEvent):void
		{
			
			if(FlatInfo.newGuiderQHWq)
			{
				this.dispatchEvent(new PanelEvent(PanelEvent.NOTNEWGUIDESD,true));
				return ;
			}
			this.setItemAry("111");
			this.controlBtnState(false,false,true);
		}
		/**
		 * 火焰武器	 
		 * @param e
		 * 
		 */	
		private function hyFun(e:ComponentEvent):void
		{
			
			if(FlatInfo.newGuiderQHWq)
			{
				this.dispatchEvent(new PanelEvent(PanelEvent.NOTNEWGUIDESD,true));
				return ;
			}
			this.setItemAry("113");
			this.controlBtnState(false,false,false,true);
		}
		/**
		 * 物理武器	 
		 * @param e
		 * 
		 */	
		private function wlFun(e:ComponentEvent):void
		{
			
			if(FlatInfo.newGuiderQHWq)
			{
				this.dispatchEvent(new PanelEvent(PanelEvent.NOTNEWGUIDESD,true));
				return ;
			}
			this.setItemAry("114");
			this.controlBtnState(false,false,false,false,true);
		}
		
		/**
		 * 毁灭武器	 
		 * @param e
		 * 
		 */	
		private function hmFun(e:ComponentEvent):void
		{
			
			if(FlatInfo.newGuiderQHWq)
			{
				this.dispatchEvent(new PanelEvent(PanelEvent.NOTNEWGUIDESD,true));
				return ;
			}
			this.setItemAry("110");
			this.controlBtnState(false,false,false,false,false,true);
		}
		
		/**
		 * 下面武器区域 加上点击事件
		 */		
		private var zbWqObj:Object=new Object();
		private function controlClickFun():void
		{
			for(var i:int=0;i<10;i++)
			{
				var wp:WeaponItemPanel=this.getChildByName("weapon"+(i+1)) as WeaponItemPanel;
				zbWqObj[wp.name]=wp;
				wp.addEventListener(MouseEvent.CLICK,clickFun);
			}
		}
		
		/**
		 * 下面武器区域 
		 */	
		public function clickFun(e:MouseEvent):void
		{
			if(!this.isHome)
			{
				this.dispatchEvent(new PanelEvent(PanelEvent.NOTINHOME,true));
				return ;
			}
			var zbwq:WeaponItemPanel=e.currentTarget as WeaponItemPanel;
			if(!zbwq || !zbwq.iuserItem)return;
			
			//新手指导是判start
			if(FlatInfo.newGuiderQHWq && zbwq.iuserItem.itemId==11200002)
			{
				this.dispatchEvent(new PanelEvent(PanelEvent.ZBSDLD,true));
			}
			
			if(FlatInfo.newGuiderQHWq && zbwq.iuserItem.itemId!=11200002)
			{
				this.dispatchEvent(new PanelEvent(PanelEvent.NOTNEWGUIDESD,true));
				return ;
			}
			
			//新手指导是判end
			
			for each(var tmp:WeaponItemPanel in this.zbWqObj)
			{
				tmp.filters=null;
				if(tmp.name==zbwq.name && !zbwq.fm.visible)
				{
					if(this.selectWeapons.length<this._bulideLv)
					{
						tmp.setWeaponOld();
						tmp.filters=[new GlowFilter(0xFFFF00,0.8,5,5,2,BitmapFilterQuality.LOW)];
						zbwq.fm.visible=true;
						this.controData(this.allArr,this.selectWeapons,zbwq.weaponId);
						tmp.filters=null;
					}
					else{
						this.dispatchEvent(new Event("selectLlessThanBuildLV",true));
					}
				}
				
			}
			
		}
		
		/**
		 * 为上面武器 区加上点击事件
		 */		
		private var xzWqObj:Object=new Object();
		private function controlXZWqClickFun():void
		{
			for(var i:int=0;i<5;i++)
			{
				var xzwp:SellectWeaponsPanel=this.getChildByName("item"+(i+1)) as SellectWeaponsPanel;
				xzWqObj[xzwp.name]=xzwp;
				xzwp.addEventListener(MouseEvent.CLICK,clickXZWqFun);
			}
		}
		
		/**
		 * 点击上面武器 区 
		 */	
		public function clickXZWqFun(e:MouseEvent):void
		{
			if(!this.isHome)
			{
				this.dispatchEvent(new PanelEvent(PanelEvent.NOTINHOME,true));
				return ;
			}
			
			if(FlatInfo.newGuiderQHWq)
			{
				this.dispatchEvent(new PanelEvent(PanelEvent.NOTNEWGUIDESD,true));
				return ;
			}
			var xzwq:SellectWeaponsPanel=e.currentTarget as SellectWeaponsPanel;
			for each(var tmp:SellectWeaponsPanel in this.xzWqObj)
			{
				tmp.filters=null;
				if(tmp.name==xzwq.name && !xzwq.fm.visible)
				{
					tmp.filters=[new GlowFilter(0xFFFFFF,0.8,5,5,2,BitmapFilterQuality.LOW)];
					xzwq.fm.visible=true;
					this.controData(this.selectWeapons,this.allArr,xzwq.weaponId);
					tmp.filters=null;
				}
			}
		}
		
		/**
		 * 点击面板进行数据交换 
		 * @param _removeArr
		 * @param _addArr
		 * @param weaponId
		 * @param clickUp
		 */		
		private function controData(_removeArr:Array,_addArr:Array,weaponId:Number):void
		{
			var rmItem:IUseItem;
			var len:Number=_removeArr.length;
			for(var i:int=0;i<len;i++)
			{
				rmItem=_removeArr[i] as IUseItem;
				if(rmItem.itemId==weaponId)
				{
					_addArr.push(rmItem);
					_removeArr.splice(i,1);
					break;
				}
			}
			this.setSelectWeaponData();
			switch(true)
			{
				case this.wqAllBtn.lockState:
					this.wqAllBtn.dispatchEvent(new ComponentEvent(ComponentEvent.BUTTON_DOWN));
					break;
				case this.wqDjBtn.lockState:
					this.wqDjBtn.dispatchEvent(new ComponentEvent(ComponentEvent.BUTTON_DOWN));
					break;
				case this.wqQsBtn.lockState:
					this.wqQsBtn.dispatchEvent(new ComponentEvent(ComponentEvent.BUTTON_DOWN));
					break;
				case this.wqHyBtn.lockState:
					this.wqHyBtn.dispatchEvent(new ComponentEvent(ComponentEvent.BUTTON_DOWN));
					break;
				case this.wqWlBtn.lockState:
					this.wqWlBtn.dispatchEvent(new ComponentEvent(ComponentEvent.BUTTON_DOWN));
					break;
				case this.wqHmBtn.lockState:
					this.wqHmBtn.dispatchEvent(new ComponentEvent(ComponentEvent.BUTTON_DOWN));
					break
				default: this.wqAllBtn.dispatchEvent(new ComponentEvent(ComponentEvent.BUTTON_DOWN));
			}
		}
		
		private function turnLeftHandler(e:ComponentEvent):void
		{
			this.nowPage-=1;
		}
		
		private function turnRightHandler(e:ComponentEvent):void
		{
			this.nowPage+=1;
		}
		
		private function updatePage():void
		{
			(this.getChildByName("txt_page") as TextField).text=this.nowPage+"/"+this.maxPage;
			setLeftAndRightVisible();
		}
		
		/*getter & setter*/
		public function get nowPage():int
		{
			return _nowPage;
		}
		public var allArr:Array=new Array();
		/**
		 * 给自己的武器装备赋值  提供给外部赋值的接口  下面区域
		 * @param obj
		 * 
		 */		
		public function setItemsAry(obj:Object):void
		{
			this.allArr=[];
			for each(var item:IUseItem in obj)
			{
				allArr.push(item);
			}
			if(FlatInfo.newGuiderQHWq)
			{
				if(allArr.length>0) this.wqDjBtn.dispatchEvent(new ComponentEvent(ComponentEvent.BUTTON_DOWN));
			}else{
				if(allArr.length>0) this.wqAllBtn.dispatchEvent(new ComponentEvent(ComponentEvent.BUTTON_DOWN));
			}
			
		}
		
		private function setItemAry(type:String="all"):void
		{
			this._itemsAry=[];
			if(type=="all")
			{
				this._itemsAry=allArr;
			}else{
				for each(var item:IUseItem in allArr)
				{
					var _type:String=String(item.itemId).substr(0,3);
					if(type==_type)
					{
						if(FlatInfo.newGuiderQHWq && type=="112" && item.itemId==11200002)
						{
							this._itemsAry.unshift(item);
						}else{
							this._itemsAry.push(item);
						}
					}
				}
			}
			
			if(!this.itemsAry.length)
			{
				this.maxPage=1;
			}
			else
			{
				this.maxPage=int((this.itemsAry.length-1)/10)+1;
			}
			if(this.nowPage>this.maxPage)
			{
				this.nowPage=this.maxPage;
			}
			else
			{
				this.reshow();
			}
		}
		

		
		public function getMakingListFrame(index:int):WeaponItemPanel
		{
			if(index<0 || index>9)throw new Error("索引出错");
			return (this.getChildByName("weapon"+(index+1))) as WeaponItemPanel;
		}
		
		public function reshow():void
		{
			for(var i:int=0;i<10;i++)
			{
				var mklist:WeaponItemPanel=this.getMakingListFrame(i);
				mklist.fm.visible=true;
				var collectItem:IUseItem=this.itemsAry[i+(this.nowPage-1)*10] as IUseItem;
				if(null!=collectItem)
				{
					mklist.setItemWeanpon(collectItem);
				}
			}
		}
		
		private function getSelectWeaponListFrame(index:int):SellectWeaponsPanel
		{
			if(index<0 || index>5)throw new Error("索引出错");
			return (this.getChildByName("item"+(index+1))) as SellectWeaponsPanel;
		}
		
		/**
		 * 存储选好的武器 数组 
		 */		
		public var selectWeapons:Array;
		
		/**
		 * 提供外面赋值的方法  (装备武器)
		 * @param obj
		 */		
		public function setSelectWeaponObjData(obj:Object):void
		{
			this.selectWeapons=[];
			for each(var item:IUseItem in obj)
			{
				selectWeapons.push(item);
			}
			setSelectWeaponData();
		}
		
		private function setSelectWeaponData():void
		{
			for(var i:int=0;i<this.bulideLv;i++)
			{
				var swp:SellectWeaponsPanel=this.getSelectWeaponListFrame(i);
				swp.fm.visible=true;
				swp.lock.visible=false;
				var swpItems:IUseItem=selectWeapons[i] as IUseItem;
				if(null!=swpItems)
				{
					swp.setSelectItemWeanpon(swpItems,i);
				}
			}
		}
		
	
		
		public function set nowPage(value:int):void
		{
			if(value==this.nowPage)return;
			if(value<1 || value>this.maxPage)return;
			_nowPage = value;
			this.updatePage();
			this.reshow();
		}
		
		public function get maxPage():int
		{
			return _maxPage;
		}
		
		public function set maxPage(value:int):void
		{
			if(value<0) return;
			_maxPage = value;
			this.updatePage();
		}
		
		public function get itemsAry():Array
		{
			return _itemsAry;
		}

		/**
		 * 武器建筑的等级 
		 */
		public function get bulideLv():Number
		{
			return _bulideLv;
		}

		/**
		 * @private
		 */
		public function set bulideLv(value:Number):void
		{
			_bulideLv = value;
		}

		public function get isHome():Boolean
		{
			return _isHome;
		}

		public function set isHome(value:Boolean):void
		{
			_isHome = value;
		}

		/**
		 *设置左、右翻页按扭是否可见 
		 */
		private function setLeftAndRightVisible():void
		{
			var filter:ColorMatrixFilter = new ColorMatrixFilter([0.3,0.6,0,0,0,0.3,0.6,0,0,0,0.3,0.6,0,0,0,0,0,0,1,0]) ;
			
			var noShowLeft:Boolean		=		this._nowPage==1;
			var noShowRight:Boolean		=		this._nowPage==this._maxPage;
			
			if(!noShowLeft){
				//				(this.getChildByName("buttonLeft") as MyButton).visible				=		true;
				(this.getChildByName("buttonLeft") as MyButton).mouseChildren			    =		true;
				(this.getChildByName("buttonLeft") as MyButton).mouseEnabled			    =		true;
				(this.getChildByName("buttonLeft") as MyButton).filters					=		null;
			}
			else
			{
				//				(this.getChildByName("buttonLeft") as MyButton)						=		false;
				(this.getChildByName("buttonLeft") as MyButton).mouseChildren				=		false;
				(this.getChildByName("buttonLeft") as MyButton).mouseEnabled				=		false;
				(this.getChildByName("buttonLeft") as MyButton).filters					=		[filter];
			}
			
			if(!noShowRight){
				//				(this.getChildByName("buttonRight") as MyButton).visible				=		true;
				(this.getChildByName("buttonRight") as MyButton).mouseChildren		=		true;
				(this.getChildByName("buttonRight") as MyButton).mouseEnabled			=		true;
				(this.getChildByName("buttonRight") as MyButton).filters				=		null;
			}
			else
			{
				//				(this.getChildByName("buttonRight") as MyButton).visible				=		false;
				(this.getChildByName("buttonRight") as MyButton).mouseChildren		=		false;
				(this.getChildByName("buttonRight") as MyButton).mouseEnabled			=		false;
				(this.getChildByName("buttonRight") as MyButton).filters				=		[filter];
			}
		}

	}
}